<?php
/**
 * @author     Thibaut Cromphaut, Gaïtano Boeraeve, Matthias Mercy
 * @copyright  Copyright (c) 2011 memorygame
 */
class Application_Model_HighscoreMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Highscores';
    }

    /**
     * Level met een bepaald id ophalen
     * 
     * @param integer $id
     * @return Application_Model_User
     */
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('highscore_id = ?' => $id));

        return $this->_toHighscore($row);
    }

    /**
     * Alle Levels ophalen
     * 
     * @return array w/ max 10 highscores rows
     * @ 
     */
    public function fetchAll()
    {        
        $order = $this->getDbTable()->select()->order(array('highscores_number DESC'));
        $rows = $this->getDbTable()->fetchAll($order);

        return $this->_toHighscores($rows);
    }
    
    /**
     * Alle Levels ophalen
     * 
     * @return array w/ max 10 highscores rows
     * @ 
     */
    public function fetchTopTen()
    {        
        
        $select = $this->getDbTable()->select()->limit(10)->order(array('highscores_number DESC'));
        $rows = $this->getDbTable()->fetchAll($select);

        return $this->_toHighscores($rows);
    }
  
    
    
    /**
     * Level opslaan
     *
     * @param Application_Model_Highscore $highscore
     */
    public function save(Application_Model_Highscore $highscore)
    {
        $data = array(                    
            'highscores_number'   =>$highscore->Number,
            'users_user_id'       =>$highscore->UserId,           
        );

        if (null == $highscore->Id) {
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('highscore_id = ?' => $highscore->Id));
        }
    }
    
    /**
     * highscore verwijderen
     */
    public function delete($id)
    {   
        if (null != $id) {  
            $this->getDbTable()->delete(array('highscore_id = ?' => $id));
        } 
    }
    
    
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_Highscore 
     */
    private function _toHighscore(Zend_Db_Table_Row_Abstract $row = null)
    {
        $highscore = new Application_Model_Highscore();
        $userMapper = new Application_Model_UserMapper();
        $user = new Application_Model_User();
        $user->Id = "3";
        $user->Email = "Ik@lol.be";

        if ($row) {
            $highscore->Id              = $row['highscore_id'];
            $highscore->Number          = $row['highscores_number'];
            $highscore->UserId          = $row['users_user_id'];            
            $highscore->User            = $userMapper->fetchById($row['users_user_id']);
        }

        return $highscore;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _toHighscores(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $highscores = array();

        if ($rows) {
            foreach ($rows as $row) {
                $highscores[] = $this->_toHighscore($row);
            }
        }
        
        return $highscores;
    }

}


